package com.eastfair.config.service.entityservice.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.config.entity.Banner;
import com.eastfair.config.entity.ConfigAttributeValue;
import com.eastfair.config.service.ConfigAttributeValueService;
import com.eastfair.config.service.entityservice.BannerService;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.projectcore.dto.ConfigAttributeDTO;
import com.eastfair.projectcore.vo.ConfigAttributeVO;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_ID;
import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_PARAMS;

/**
 * 轮播图服务
 * Date:2021/8/7
 * Time:15:45
 *
 * @author zjn
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private ProjectCoreServiceFeign projectCoreServiceFeign;
    @Autowired
    private ConfigAttributeValueService configAttributeValueService;

    @Override
    public List<ConfigAttributeVO> getAttributes() {
        ConfigAttributeDTO configAttributeDTO = new ConfigAttributeDTO();
        configAttributeDTO.setEntityClass(Banner.class.getName());
        return projectCoreServiceFeign.listByEntityClass(configAttributeDTO).getData();
    }

    @Override
    public List<Banner> getList(Banner banner) {
        List<Banner> bannerList = configAttributeValueService.listEntityByClass(Banner.class);
        if (bannerList == null || bannerList.isEmpty()) {
            return null;
        }
        if (Objects.isNull(banner)) {
            return bannerList;
        }
        // 选择性过滤
        return bannerList.stream().filter(s -> compare(s, banner)).collect(Collectors.toList());
    }

    @Override
    public R<Banner> get(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }
        Banner banner = Banner.builder().flagId(flagId).build();
        List<Banner> bannerList = getList(banner);
        if (bannerList != null && !bannerList.isEmpty()) {
            return R.success(bannerList.get(0));
        }
        return R.success(null);
    }

    @Override
    public R<Boolean> saveEntity(Banner banner) {
        if (Objects.isNull(banner.getTitle())) {
            return R.fail(ERROR_NULL_PARAMS);
        }
        banner.setExhibitionId(ContextUtil.getExhibitionId());
        return R.success(configAttributeValueService.saveEntity(banner));
    }

    @Override
    public R<Boolean> deleteEntity(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }

        UpdateWrapper<ConfigAttributeValue> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ConfigAttributeValue::getProjectId, ContextUtil.getProjectId())
                .eq(ConfigAttributeValue::getFlagId, flagId).set(ConfigAttributeValue::getIsDelete, BusinessConstant.DELETE_YES);
        return R.success(configAttributeValueService.update(updateWrapper));
    }

    @Override
    public R<Boolean> updateEntity(Banner banner) {
        if (Objects.isNull(banner.getFlagId())) {
            return R.fail(ERROR_NULL_ID);
        }
        // 标识性参数不可修改
        banner.setSubjectType(null);
        banner.setLanguageType(null);
        return R.success(configAttributeValueService.updateEntity(banner));
    }

    /**
     * 轮播图过滤器
     *
     * @param target   目标值
     * @param standard 过滤条件
     * @return
     */
    private Boolean compare(Banner target, Banner standard) {
        if (Objects.isNull(target) || Objects.isNull(standard)) {
            return true;
        }

        if (!Objects.isNull(standard.getFlagId())) {
            if (!standard.getFlagId().equals(target.getFlagId())) {
                return false;
            }
        }

        if (!StringUtils.isBlank(standard.getSubjectType())) {
            if (!standard.getSubjectType().equals(target.getSubjectType())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getLanguageType())) {
            if (!standard.getLanguageType().equals(target.getLanguageType())) {
                return false;
            }
        }
        if (!Objects.isNull(standard.getIsEnabled())) {
            if (!standard.getIsEnabled().equals(target.getIsEnabled())) {
                return false;
            }
        }

        return true;
    }
}